<?php
//
//	file: includes/attach/class_attach_handler.php
//	author: ptirhiik
//	begin: 05/03/2006
//	version: 1.6.2 - 13/06/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
	exit;
}

class attach_handler extends file_handler
{
	function attach_handler()
	{
		global $config;
		parent::file_handler($config->globals['mod_attachment_CH']['config']->data);
	}

	function remove_users($ids)
	{
		global $db;

		if ( !$ids )
		{
			return false;
		}

		// chunck in $rows_a_turn number of attach to spare our memory
		// this won't prevent a timeout on large prune, but should avoid memory overflow
		// first deals with pms
		$rows_a_turn = 300;
		$more = false;
		while ( $more )
		{
			$sql = 'SELECT DISTINCT attach_id
						FROM ' . ATT_LINKS_TABLE . '
						WHERE privmsgs_id IN(' . $db->sql_subquery('privmsgs_id', '
								SELECT DISTINCT privmsgs_id
									FROM ' . PRIVMSGS_TABLE . '
									WHERE ' . (!is_array($ids) ? 'privmsgs_from_userid = ' . intval($ids) : 'privmsgs_from_userid IN(' . implode(', ', $ids) . ')') . '
										OR ' . (!is_array($ids) ? 'privmsgs_to_userid = ' . intval($ids) : 'privmsgs_to_userid IN(' . implode(', ', $ids) . ')') . '
							', __LINE__, __FILE__) . ')
						LIMIT ' . ($rows_a_turn + 1);
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			unset($sql);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$attach_ids[] = intval($row['attach_id']);
			}
			$db->sql_freeresult($result);
			if ( ($more = count($attach_ids) > $rows_a_turn) )
			{
				unset($attach_ids[$rows_a_turn]);
			}
			if ( $attach_ids )
			{
				$this->delete_attach($attach_ids);
				unset($attach_ids);
			}
		}

		// re-affect user deleted attachments to ANONYMOUS (posts attachments)
		$sql = 'UPDATE ' . ATT_LINKS_TABLE . '
					SET user_id = ' . ANONYMOUS . '
					WHERE ' . (!is_array($ids) ? 'user_id = ' . intval($ids) : 'user_id IN(' . implode(', ', $ids) . ')');
		$db->sql_query($sql, false, __LINE__, __FILE__);

		// delete users quotas
		$sql = 'DELETE FROM ' . ATT_QUOTA_TABLE . '
					WHERE group_id IN(' . $db->sql_subquery('group_id', '
						SELECT DISTINCT group_id
							FROM ' . GROUPS_TABLE . '
							WHERE ' . (!is_array($ids) ? 'group_user_id = ' . intval($id) : 'group_user_id IN(' . implode(', ', $ids) . ')') . '
						', __LINE__, __FILE__) . ')';
		$db->sql_query($sql, false, __LINE__, __FILE__);
	}

	function remove_topics($ids)
	{
		global $db;

		// chunck in $rows_a_turn number of attach to spare our memory
		// this won't prevent a timeout on large prune, but should avoid memory overflow
		$rows_a_turn = 300;
		$more = false;
		while ( $more )
		{
			// get the attach_id to remove
			$attach_ids = array();
			$sql = 'SELECT l.attach_id
						FROM ' . ATT_LINKS_TABLE . ' l, ' . POSTS_TABLE . ' p
						WHERE l.post_id = p.post_id
							AND ' . (!is_array($ids) ? 'p.topic_id = ' . intval($ids) : 'p.topic_id IN(' . implode(', ', $ids) . ')') . '
						LIMIT ' . ($rows_a_turn + 1);
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$attach_ids[] = intval($row['attach_id']);
			}
			$db->sql_freeresult($result);
			if ( ($more = count($attach_ids) > $rows_a_turn) )
			{
				unset($attach_ids[$rows_a_turn]);
			}
			if ( $attach_ids )
			{
				$this->delete_attach($attach_ids);
			}
			unset($attach_ids);
		}
		return true;
	}

	function remove_posts($ids)
	{
		global $db;

		if ( !$ids )
		{
			return false;
		}

		// chunck in $rows_a_turn number of attach to spare our memory
		// this won't prevent a timeout on large prune, but should avoid memory overflow
		$rows_a_turn = 300;
		$more = false;
		while ( $more )
		{
			$sql = 'SELECT attach_id
						FROM ' . ATT_LINKS_TABLE . '
						WHERE ' . (!is_array($_ids) ? 'post_id = ' . intval($ids) : 'post_id IN(' . implode(', ', $ids) . ')') . '
						LIMIT ' . ($rows_a_turn + 1);
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$attach_ids[] = intval($row['attach_id']);
			}
			$db->sql_freeresult($result);
			if ( ($more = count($attach_ids) > $rows_a_turn) )
			{
				unset($attach_ids[$rows_a_turn]);
			}
			if ( $attach_ids )
			{
				$this->delete_attach($attach_ids);
				unset($attach_ids);
			}
		}
		return true;
	}

	function remove_pms($ids)
	{
		global $db;

		if ( !$ids )
		{
			return false;
		}

		// chunck in $rows_a_turn number of attach to spare our memory
		// this won't prevent a timeout on large prune, but should avoid memory overflow
		$rows_a_turn = 300;
		$more = false;
		while ( $more )
		{
			$sql = 'SELECT DISTINCT attach_id
						FROM ' . ATT_LINKS_TABLE . '
						WHERE privmsgs_id IN(' . implode(', ', $ids) . ')
							AND privmsgs_id <> 0
						LIMIT ' . ($rows_a_turn + 1);
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$attach_ids[] = intval($row['attach_id']);
			}
			$db->sql_freeresult($result);
			if ( ($more = count($attach_ids) > $rows_a_turn) )
			{
				unset($attach_ids[$rows_a_turn]);
			}
			if ( $attach_ids )
			{
				$this->delete_attach($attach_ids);
				unset($attach_ids);
			}
		}
		return true;
	}

	function delete_attach(&$attach_ids)
	{
		global $db;

		// delete files
		$sql = 'SELECT physical_filename, thumbnail
					FROM ' . ATT_DESC_TABLE . '
					WHERE attach_id IN(' . implode(', ', $attach_ids) . ')';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		if ( $db->sql_numrows($result) )
		{
			$this->open_channel();
			while ( $row = $db->sql_fetchrow($result) )
			{
				$this->unlink($row['physical_filename'], $row['thumbnail']);
			}
			$this->close_channel();
		}
		$db->sql_freeresult($result);

		// delete desc
		$sql = 'DELETE FROM ' . ATT_DESC_TABLE . '
					WHERE attach_id IN(' . implode(', ', $attach_ids) . ')';
		$db->sql_query($sql, false, __LINE__, __FILE__);

		// delete links
		$sql = 'DELETE FROM ' . ATT_LINKS_TABLE . '
					WHERE attach_id IN(' . implode(', ', $attach_ids) . ')';
		$db->sql_query($sql, false, __LINE__, __FILE__);
	}

	function sync($_ids)
	{
		global $db;

		$_ids = empty($_ids) ? array() : (!is_array($_ids) ? array(intval($_ids)) : $_ids);
		if ( empty($_ids) )
		{
			return false;
		}

		// get post ids & topic ids with attachments
		$sql = 'SELECT p.topic_id, p.post_id
					FROM ' . POSTS_TABLE . ' p, ' . ATT_LINKS_TABLE . ' a
					WHERE p.topic_id IN(' . implode(', ', $_ids) . ')
						AND a.post_id = p.post_id
						AND a.privmsgs_id = 0
					GROUP BY p.topic_id, p.post_id';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$post_ids = array();
		$topic_ids = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			$post_ids[] = intval($row['post_id']);
			$topic_ids[ intval($row['topic_id']) ] = true;
		}
		$db->sql_freeresult($result);
		$topic_ids = empty($topic_ids) ? array() : array_keys($topic_ids);

		$sql = 'UPDATE ' . POSTS_TABLE . '
					SET post_attachment = 0
					WHERE topic_id IN(' . implode(', ', $_ids) . ')
						AND post_attachment = 1' . (empty($post_ids) ? '' : '
						AND post_id NOT IN(' . implode(', ', $post_ids) . ')');
		$db->sql_query($sql, false, __LINE__, __FILE__);

		if ( !empty($post_ids) )
		{
			$sql = 'UPDATE ' . POSTS_TABLE . '
						SET post_attachment = 1
						WHERE post_id IN(' . implode(', ', $post_ids) . ')
							AND post_attachment = 0';
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
		unset($post_ids);

		// sync topic
		$sql = 'UPDATE ' . TOPICS_TABLE . '
					SET topic_attachment = 0
					WHERE topic_id IN(' . implode(', ', $_ids) . ')
						AND topic_attachment = 1' . (empty($topic_ids) ? '' : '
						AND topic_id NOT IN(' . implode(', ', $topic_ids) . ')');
		$db->sql_query($sql, false, __LINE__, __FILE__);
		if ( !empty($topic_ids) )
		{
			$sql = 'UPDATE ' . TOPICS_TABLE . '
						SET topic_attachment = 1
						WHERE topic_id IN(' . implode(', ', $topic_ids) . ')
							AND topic_attachment = 0';
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
		unset($topic_ids);
		return true;
	}
}

?>